<div style="font-size: 80%;">
<form onsubmit="this.eva.click();return false;">
<input type="text" id="expression" name="expression" />
<input type="button" id="eva" name="eva" onclick="trigger( this )" value="Evaluate!" />
</form>
<div>Result :</div>
<div id="result" style="border: 1px solid #cccccc; padding: 0px 3px 0px 3px;">&nbsp;</div>
<div>
    <p>Usage : Enter your Mathematical Expression, Click Evaluate OR Hit Enter</p>
    <p>E.g. : | sin( 270d ) - 1 | * 20 / 4 - tan( Pi/2 )</p>
    <p>
    User Guide available at
    <a href="http://code.google.com/p/misc-app/wiki/MathsExpEvalUserGuide" target="_blank">here..</a>
    </p>
</div>
</div>

<script type="text/javascript">

// trim all whitespace
function trim_whitespace( exp ) {
    return exp.replace( /\s+/g, '' );
}

// degree to radian
function deg2rad( deg ) {
    return ( deg / 180 * Math.PI );
}
function translateDeg2Rad( exp ) {
    var degRegEx = /(\d+(?:\.\d+)?)d/i;

    // while expression has degree value,
    // translate to radian one by one
    while ( degRegEx.test( exp ) ) {

        var deg = exp.match( degRegEx ).shift();
        var val = /(.+)d/.exec( deg )[1];
        var rad = deg2rad( val );

        exp = exp.replace( deg, rad );
    }

    return exp;
}

// support absolute value |...|
function translateAbsolute( exp ) {
    var absRegEx = /(\|[^\|]+\|)/ig;

    while ( absRegEx.test( exp ) ) {

        var sub_exp = exp.match( absRegEx ).shift();
        var abs_exp = /\|(.+)\|/.exec( sub_exp )[1];

        exp = exp.replace( sub_exp, 'abs(' + abs_exp + ')' );
    }

    return exp;
}

var calc = {
    'pi' : Math.PI,
    'sin' : function( param ) {
        return Math.sin( param );
    },
    'cos' : function( param ) {
        return Math.cos( param );
    },
    'tan' : function( param ) {
        return Math.tan( param );
    },
    'asin' : function( param ) {
        return Math.asin( param );
    },
    'acos' : function( param ) {
        return Math.acos( param );
    },
    'atan' : function( param ) {
        return Math.atan( param );
    },
    'abs' : function( param ) {
        return Math.abs( param );
    }
};

// this is where the maths expression evaluation kick off
function trigger( evt_src ) {

    // pre-processing expression
    var exp = trim_whitespace( evt_src.form.expression.value ).toLowerCase();
    exp = translateDeg2Rad( exp );
    exp = translateAbsolute( exp );

    var result;
    try {
        // evaluate expression with defined calculator function
        with ( calc ) {
            result = eval( exp );

            // if result is floating point, convert to fixed value
            if ( result.toString().match( /\./ ) ) {
                result = result.toFixed(9);
            }
        }
    } catch (e) {
    }

    if ( isNaN( result ) ) {
        result = 'calculation error, '
            + 'please check whether you made '
            + 'wrong expression like division by 0';
    }

//    _ById( "result" ).innerHTML = result;
    document.getElementById( "result" ).innerHTML = result;
}

</script>
